package com.example.sunmiv1.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.AddAllocationActivity;
import com.example.sunmiv1.activity.AllocationDetailActivity;
import com.example.sunmiv1.activity.RefuseDetailActivity;
import com.example.sunmiv1.entity.AllocationListInfo;
import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllocationAdapter extends BaseAdapter {

    private Context context;
    private List<AllocationListInfo.DataBean> list;
    private ViewHolder viewHolder;
    private Intent intent;
    private DecimalFormat df = new DecimalFormat("0.00");
    private String pricePermission;// 門店查看價格權限
    private String userType;// 用戶身份
    private String type;

    public AllocationAdapter(Context context, List<AllocationListInfo.DataBean> list, String type) {
        this.context = context;
        this.list = list;
//        this.warehouseStutas = stutas;
        this.type = type;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
        userType = aCache.getAsString("userType");
        LogUtil.i("userType:" + userType);
    }

    public void putType(String type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.deposit_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AllocationListInfo.DataBean dataBean = list.get(position);

        //貨至狀態
        Tools.hideViewInItem(dataBean.getWarehousing_name(), viewHolder.statusDepositAdapter);

        //操作人
        Tools.hideViewInItem(dataBean.getOperator(), viewHolder.peopleDepositAdapter);

        //出庫門店
        viewHolder.storeNameDepositAdapter.setText(dataBean.getWarehouse_name());
        //出庫時間
        viewHolder.timeDepositAdapter.setText(TextUtils.isEmpty(dataBean.getAdd_time()) ? dataBean.getWarehouse_time()
                : dataBean.getAdd_time());
        //出庫單號
        viewHolder.orderNoDepositAdapter.setText(dataBean.getOrder_number());
        //實出金額
        viewHolder.moneyDepositAdapter.setText(df.format(dataBean.getMoney_total()));
        //出庫類型
        viewHolder.typeDepositAdapter.setText(dataBean.getType_name());
        //入庫詳情-出庫詳情
        if ("1".equals(dataBean.getWarehousing_status())) {
            viewHolder.detail1DepositAdapter.setVisibility(View.VISIBLE);//入庫詳情-顯示
            viewHolder.linearTimeWarehousingAdapter.setVisibility(View.VISIBLE);//入庫時間-顯示
            viewHolder.linearTotalWarehousingAdapter.setVisibility(View.VISIBLE);//實收合計-顯示
            viewHolder.linearMoneyInAdapter.setVisibility(View.GONE);//實收金額-顯示
            viewHolder.detail1DepositAdapter.setText(R.string.WarehousingDetail);
            viewHolder.detail1DepositAdapter.setVisibility(View.VISIBLE);//入庫詳情-顯示
//            ((View)viewHolder.infoMoneyDifference.getParent()).setVisibility(View.GONE); // 盈虧  隱藏
//            viewHolder.linearGoodsArrive.setVisibility(View.GONE);//貨至-隱藏
        } else {
            if (("拒絕").equals(dataBean.getWarehousing_name())) {
                viewHolder.detail1DepositAdapter.setText(R.string.Reallocation);
                viewHolder.detail1DepositAdapter.setVisibility(View.VISIBLE);//入庫詳情-顯示
            } else {
//                viewHolder.detail1DepositAdapter.setVisibility(View.GONE);//入庫詳情-顯示
                viewHolder.detail1DepositAdapter.setVisibility(View.GONE);//入庫詳情-隱藏
            }
            viewHolder.linearTimeWarehousingAdapter.setVisibility(View.GONE);//入庫時間-隱藏
            viewHolder.linearTotalWarehousingAdapter.setVisibility(View.GONE);//實收合計-隱藏
            viewHolder.linearMoneyInAdapter.setVisibility(View.GONE);//實收金額-隱藏
//            viewHolder.linearGoodsArrive.setVisibility(View.VISIBLE);//貨至-顯示
        }
        //入庫時間
        Tools.hideViewInItem(dataBean.getWarehouse_time(), viewHolder.timeWarehousingAdapter);
        //實收合計
        viewHolder.totalWarehousingAdapter.setText(dataBean.getReal_goods_species() + "種");
        //實收金額
        viewHolder.moneyInAdapter.setText(dataBean.getReal_money_total() + "");
        final int index = position;
        viewHolder.detailDepositAdapter.setOnClickListener(v -> {//出庫詳情
            intent = new Intent(context, AllocationDetailActivity.class);  //
            intent.putExtra("id", list.get(index).getId());
            intent.putExtra("type", 2);//出庫詳情
            context.startActivity(intent);
        });
        viewHolder.detail1DepositAdapter.setOnClickListener(v -> {//入庫&重新調撥
            if (("拒絕").equals(dataBean.getWarehousing_name())) {
                intent = new Intent(context, AddAllocationActivity.class);
                intent.putExtra("id", list.get(index).getId());
                intent.putExtra("AlloOrOut", "allo");
                context.startActivity(intent);
            } else {//入庫詳情
                intent = new Intent(context, AllocationDetailActivity.class);
                intent.putExtra("id", list.get(index).getWarehouse_record_id());
                intent.putExtra("type", 1);//入庫詳情
                context.startActivity(intent);
            }
        });
        //貨至(商戶)
        Tools.hideViewInItem(dataBean.getStore_name(), viewHolder.goodsArrive);
        //貨至(門店)
        Tools.hideViewInItem(dataBean.getGoods_to_name(), viewHolder.goodsArriveShop);
        //實出合計
        viewHolder.totalDepositAdapter.setText(dataBean.getGoods_species() + "種");
        //成本合計
        viewHolder.totalCostAdapter.setText(dataBean.getTotal_cost_price() + "");
        // 盈虧金額
        if (dataBean.getDifference_money() != 0) {
            viewHolder.infoMoneyDifference.setText(dataBean.getDifference_money() + "");
        } else {
            ((View) viewHolder.infoMoneyDifference.getParent()).setVisibility(View.GONE);
        }
        if ("2".equals(dataBean.getRefuse_status())) {
            Tools.hideViewInItem(dataBean.getRefuse_goods_species(), viewHolder.refuseGoodsSpecies);
            Tools.hideViewInItem(dataBean.getRefuse_total_cost_money(), viewHolder.refuseTotalCostMoney);
            Tools.hideViewInItem(dataBean.getRefuse_total_stock_money(), viewHolder.refuseTotalStockMoney);
        }
        LogUtil.i("userType:" + userType + "   type:" + type);
        // 商戶門店申請調撥  隱藏盈虧金額
        if ("1".equals(userType) && "3".equals(type)) {
            LogUtil.i("商戶門店申請調撥  隱藏盈虧金額");
            ((View) viewHolder.infoMoneyDifference.getParent()).setVisibility(View.GONE);
        }
        //備份
        Tools.hideViewInItem(dataBean.getRemarks(), viewHolder.infoRemark);
        if ("2".equals(pricePermission) && "2".equals(userType)) {
            viewHolder.linearTotalCost.setVisibility(View.GONE);
            viewHolder.linearMoneyDeposit.setVisibility(View.GONE);
            viewHolder.linearMoneyInAdapter.setVisibility(View.GONE);
            ((View) viewHolder.infoMoneyDifference.getParent()).setVisibility(View.GONE);
            ((View) viewHolder.refuseTotalCostMoney.getParent()).setVisibility(View.GONE);
        }
        if ("2".equals(dataBean.getRefuse_status())) {
            viewHolder.refuseDetailDepositAdapter.setVisibility(View.VISIBLE);
        } else {
            viewHolder.refuseDetailDepositAdapter.setVisibility(View.GONE);
        }
        viewHolder.refuseDetailDepositAdapter.setOnClickListener(v -> {//拒收詳情
            intent = new Intent(context, RefuseDetailActivity.class);
            intent.putExtra("flag", 2);
            intent.putExtra("stock_out_id", dataBean.getId());
            intent.putExtra("userType", userType);
            context.startActivity(intent);
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.status_depositAdapter)
        TextView statusDepositAdapter;
        @BindView(R.id.linear_status_deposit)
        LinearLayout linearStatusDeposit;
        @BindView(R.id.people_depositAdapter)
        TextView peopleDepositAdapter;
        @BindView(R.id.linear_people_deposit)
        LinearLayout linearPeopleDeposit;
        @BindView(R.id.goods_arrive)
        TextView goodsArrive;
        @BindView(R.id.linear_goods_arrive)
        LinearLayout linearGoodsArrive;
        @BindView(R.id.goods_arrive_shop)
        TextView goodsArriveShop;
        @BindView(R.id.linear_goods_arrive_shop)
        LinearLayout linearGoodsArriveShop;
        @BindView(R.id.type_depositAdapter)
        TextView typeDepositAdapter;
        @BindView(R.id.linear_type_deposit)
        LinearLayout linearTypeDeposit;
        @BindView(R.id.orderNo_depositAdapter)
        TextView orderNoDepositAdapter;
        @BindView(R.id.linear_orderNo_deposit)
        LinearLayout linearOrderNoDeposit;
        @BindView(R.id.time_depositAdapter)
        TextView timeDepositAdapter;
        @BindView(R.id.linear_time_deposit)
        LinearLayout linearTimeDeposit;
        @BindView(R.id.time_warehousingAdapter)
        TextView timeWarehousingAdapter;
        @BindView(R.id.linear_time_warehousingAdapter)
        LinearLayout linearTimeWarehousingAdapter;
        @BindView(R.id.storeName_depositAdapter)
        TextView storeNameDepositAdapter;
        @BindView(R.id.total_depositAdapter)
        TextView totalDepositAdapter;
        @BindView(R.id.linear_total_deposit)
        LinearLayout linearTotalDeposit;
        @BindView(R.id.total_costAdapter)
        TextView totalCostAdapter;
        @BindView(R.id.linear_total_cost)
        LinearLayout linearTotalCost;
        @BindView(R.id.money_depositAdapter)
        TextView moneyDepositAdapter;
        @BindView(R.id.linear_money_deposit)
        LinearLayout linearMoneyDeposit;
        @BindView(R.id.money_inAdapter)
        TextView moneyInAdapter;
        @BindView(R.id.linear_money_inAdapter)
        LinearLayout linearMoneyInAdapter;
        @BindView(R.id.total_warehousingAdapter)
        TextView totalWarehousingAdapter;
        @BindView(R.id.linear_total_warehousingAdapter)
        LinearLayout linearTotalWarehousingAdapter;
        @BindView(R.id.info_money_difference)
        TextView infoMoneyDifference;
        @BindView(R.id.refuse_goods_species)
        TextView refuseGoodsSpecies;
        @BindView(R.id.refuse_total_cost_money)
        TextView refuseTotalCostMoney;
        @BindView(R.id.refuse_total_stock_money)
        TextView refuseTotalStockMoney;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.layout_remark)
        RelativeLayout layoutRemark;
        @BindView(R.id.detail_depositAdapter)
        TextView detailDepositAdapter;
        @BindView(R.id.refuseDetail_depositAdapter)
        TextView refuseDetailDepositAdapter;
        @BindView(R.id.detail1_depositAdapter)
        TextView detail1DepositAdapter;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
